package pfc.taller.prueba;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import pfc.taller.comun.Dia;
import pfc.taller.comun.MapeoIbatis;
import pfc.taller.comun.Mes;
import pfc.taller.comun.Semana;

public class Prueba0 {

	/**
	 * @param args
	 */
	final int DIAS_SEMANA 	= 7;
	
	
	public static void main(String[] args)
	{
	
		Date fecha		= MapeoIbatis.mapeoAFecha("31/08/2008");
		Dia dia			= new Dia();
		Semana semana	= new Semana();
		Calendar cal	= Calendar.getInstance();
		cal.setTime(fecha);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		
	
		int dia_semana 					= cal.get(Calendar.DAY_OF_WEEK) - 1 ;
		SimpleDateFormat formatterMes 	= new SimpleDateFormat("MM");
		
		semana.setMes(new Mes( formatterMes.format(cal.getTime()) ));
		semana.setAnyo( cal.get(Calendar.YEAR) );
		
		if ( dia_semana == 0)
		{
			dia_semana	= 7;
		}
		int aux = dia_semana;
		int dia_mes		= cal.get(Calendar.DAY_OF_MONTH);
		dia 			= new Dia(dia_mes, dia_semana -1, 0);
		
		semana.getListaDias().set(dia_semana - 1, dia);
		
		for(int i = dia_semana; i < 7 ; i++)
		{
			cal.add(Calendar.DAY_OF_YEAR, + 1);
			
			dia_mes 	= cal.get(Calendar.DAY_OF_MONTH);
			dia 		= new Dia(dia_mes,dia_semana ,  1);
			
			semana.getListaDias().set(dia_semana , dia);
			dia_semana++;
		}
		
		semana.setFECHAF( cal.getTime() );
		cal.setTime(fecha);
		if (aux!=1)
		{
			for(int i = aux-1; i >= 1 ; i--)
			{
				cal.add(Calendar.DAY_OF_YEAR, - 1);
				
				dia_mes 	= cal.get(Calendar.DAY_OF_MONTH);
				dia 		= new Dia(dia_mes,aux-2 ,  -1);
				
				semana.getListaDias().set(aux-2 , dia);
				aux--;
			}
		}
		
		semana.setFECHAI( cal.getTime() );		
		
		System.out.println( semana );
	}

}
